/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-04-06
 * Time: 20:22
 */
public class Myqueue11 {
    class ListNode {
        ListNode prev;
        ListNode next;
        public int val;

        public ListNode(int val) {
            this.val = val;
        }
    }
    ListNode head;
    ListNode last;


    public void offer(int val) {
        ListNode newnode = new ListNode(val);
        if(head == null) {
            head = last = newnode;
            return;
        }
        //尾插
        last.next = newnode;
        newnode.prev = last;
        last = newnode;
    }

    public int poll() {
        if(head == null) {
           try {
               checkNullPointer();
           }catch(MyNullPointerException m) {
               m.printStackTrace();
           }
        }
        int val = head.val;
        if(head.next == null) {
            head = last = null;
        }else {
            head = head.next;
            head.prev = null;
        }
        return val;
    }

    public int peek() {
        if(head == null) {
            try {
                checkNullPointer();
            }catch(MyNullPointerException m) {
                m.printStackTrace();
            }
        }
        return head.val;
    }

    public boolean isEmpty() {
        return head == null;
    }

    public void checkNullPointer() throws MyNullPointerException{
        throw new MyNullPointerException("head为null");
    }



}
